<?php

namespace app\logic\single;

use app\api\model\music\MusicApiModel;
use app\api\model\music\MusicDjApiModel;
use app\api\model\music\MusicDjTeachingApiModel;
use app\api\model\music\MusicSelectedPackApiModel;
use app\api\model\music\MusicSkewerApiModel;
use app\api\model\user\MusicCollectionApiModel;
use app\api\model\user\UserGoodsApiModel;
use think\Request;
use think\facade\Db;

class GoodsSingle
{


    private static $inc;
    /**
     * @var MusicApiModel
     */
    private $musicModel;
    /**
     * @var MusicDjApiModel
     */
    private $musicDjModel;
    /**
     * @var MusicDjTeachingApiModel
     */
    private $musicDjTeacherModel;
    /**
     * @var MusicSelectedPackApiModel
     */
    private $musicSelectedPackModel;
    /**
     * @var MusicSkewerApiModel
     */
    private $musicSkewerModel;

    /**
     * 根据goodsList 获取music信息
     * @param array $goods_list
     * @param bool $is_unique 是否去重
     * @return array
     */
    public function getMusicByGoodsList(array $goods_list,bool $is_unique = true): array
    {
        $music_arr = [];
        foreach ($goods_list as $goods_val){
            switch ($goods_val['type']){
                case 10:
                    $music_arr[]=[
                        'id'=>$goods_val['goods']['id'],
                        'title'=>$goods_val['goods']['title'],
                        'url'=>$goods_val['goods']['url'],
                    ];
                    break;
                case 20:
                case 30:
                case 50:
                    $music_arr[]=[
                        'id'=>$goods_val['goods']['music_id'],
                        'title'=>$goods_val['goods']['music']['title'],
                        'url'=>$goods_val['goods']['music']['url'],
                    ];
                    break;
                case 40:
                    $detail = $goods_val['goods']['detail'];
                    foreach ($detail as $detail_val){
                        if(empty($detail_val['music'])){
                            $detail_val['music']['title']='被删除';
                            $detail_val['music']['url']='';
                        }
                        $music_arr[]=[
                            'id'=>$detail_val['music_id'],
                            'title'=>$detail_val['music']['title'],
                            'url'=>$detail_val['music']['url'],
                        ];

                    }
                    break;
            }
        }
        if($is_unique){
            $music_arr = array_values(array_column($music_arr, NULL, 'id'));
        }
        return $music_arr;
    }

    public function isCollection($id, int $type=10)
    {
        global $user;
        if(empty($user)){
            return false;
        }
        if(MusicCollectionApiModel::where('user_id',$user['id'])
            ->where('goods_id',$id)
            ->where('type',$type)
            ->value('id')){
            return true;
        }else{
            return false;
        }

    }

    private function __clone(){

    }

    public static function getInstance(){
        if(is_null(self::$inc)){
            self::$inc = new self();
        }
        return self::$inc;
    }
    function __construct()
    {
        $this->musicModel = new MusicApiModel();
        $this->musicDjModel = new MusicDjApiModel();
        $this->musicDjTeacherModel = new MusicDjTeachingApiModel();
        $this->musicSelectedPackModel = new MusicSelectedPackApiModel();
        $this->musicSkewerModel = new MusicSkewerApiModel();
    }

    public function getGoodsByType($goods_id,$type){
        [$model,$field] = $this->getModelByType($type);
        if($model === false){
            return false;
        }
        $goods = $model->field($field)->where('id',$goods_id)->find();
        if(empty($goods)){
            return [];
        }
        return $goods;
    }



    public function getGoodsList(array $list,bool $writer_rate=false,bool $pan = false)
    {
        foreach ($list as &$val){
            [$model,$field] = $this->getModelByType($val['type']??0,$writer_rate,$pan);
            if($model === false){
                $val['goods'] = [
                    'title'=>'已删除',
                ];
                continue;
            }
            $val['goods'] = $model->details($val['goods_id'],$field)?:['title'=>'已删除'];
        }
        unset($val);
        return $list;
    }
    public function getGoods($goods_id,$type,bool $writer_rate=false,bool $pan = false)
    {
        [$model,$field] = $this->getModelByType($type??0,$writer_rate,$pan);
        if($model === false){
            $goods['title'] = '已删除';
        }else{
            $goods = $model->details($goods_id,$field)?:['title'=>'已删除'];
        }
        return $goods;
    }
    function getModelByType(int $type,bool $writer_rate = false,bool $pan = false){
        $filed= 'id,title,price,original_price,play_num,sale,img';
        switch ($type){
            case 10://单曲
                $model = $this->musicModel;
                $filed.=',user_id,url';
                if($writer_rate){
                    $filed.=',writer_rate';
                }
                break;
            case 20://独家dj单曲
                if($writer_rate){
                    $filed.=',writer_rate';
                }
                $model = $this->musicDjModel;
                $filed.=',music_id,bmp,key';
                break;
            case 30://dj教学
                $model = $this->musicDjTeacherModel;
                $filed.=',music_id';
                break;
            case 40://精选音乐包
                if($pan){
                    $filed.=',pan_url,pan_pwd';
                }
                $filed.=',type_id';
                $model = $this->musicSelectedPackModel;
                break;
            case 50://套曲串烧
                $filed.=',music_id';
                if($writer_rate){
                    $filed.=',writer_rate';
                }
                if($pan){
                    $filed.=',pan_url,pan_pwd';
                }
                $model = $this->musicSkewerModel;
                break;
            default:
                $model = false;
                break;
        }
        return [$model,$filed];
    }

    function getModelNameByType(int $type){
        switch ($type){
            case 10://单曲
                $model = $this->musicModel;
                break;
            case 20://独家dj单曲
                $model = $this->musicDjModel;
                break;
            case 30://dj教学
                $model = $this->musicDjTeacherModel;
                break;
            case 40://精选音乐包
                $model = $this->musicSelectedPackModel;
                break;
            case 50://套曲串烧
                $model = $this->musicSkewerModel;
                break;
            default:
                return false;
        }
        return $model;
    }


    function isHave($goods_id,$type,$user_id){
        if(empty($user_id)){
            return false;
        }
        return UserGoodsApiModel::isHave($goods_id,$type,$user_id);
    }

    function IncPlayNum($goods_id,$type,$music_id){
        [$model,$filed] = $this->getModelByType($type);
        if($model === false){
            return '失败';
        }
        $info = $model->where('id',$goods_id)->inc('play_num')->update();
        //增加音乐播放
        if($type != 10){//如果等于10，则就是单曲+1，上面就已经处理了
            $a=$this->musicModel->where('id',$music_id)->inc('play_num')->update();
        }
        return true;
    }

    public function IncSales(array $inc_goods_sale)
    {
        foreach ($inc_goods_sale as $val){
            [$model,$filed] = $this->getModelByType($val['type']);
            if($model === false){
                return '失败';
            }
            $model->where('id',$val['goods_id'])->inc('sale')->update();
        }
        return true;
    }

}